﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.Generic;
using System.Reflection;

namespace uTorrentSL.Converters
{
    public class TimeSpanConvert : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TimeSpan ts = TimeSpan.Parse(value.ToString());
            int leftDays = 0;
            string result = "∞";
            if (ts.Days >= 365)
                return result;
            else
                result = "";
            if (ts.Days > 30)
            {
                result = Math.Round((decimal)ts.Days / 30).ToString() + "M";
                leftDays = ts.Days % 30;
                if (leftDays > 7)
                {
                    remainingDays(ref result, ref leftDays);
                }
            }
            else if (ts.Days > 7)
            {
                remainingDays(ref result, ref leftDays);
            }
            else if (ts.Days > 0)
            {
                leftDays = ts.Days;
                result += leftDays + "d";
                result += " " + ts.Hours + "h";
            }
            else if (ts.Hours > 0)
            {
                result += ts.Hours + "h";
                result += " " + ts.Minutes + "m";
            }
            else
            {
                result += ts.Minutes + "m";
                result += " " + ts.Seconds + "s";
            }
            return result;
            
        }

        private static void remainingDays(ref string result, ref int leftDays)
        {
            result += " " + Math.Round((decimal)leftDays / 7).ToString() + "d";
            leftDays = leftDays % 7;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }


    public class FeedUrlConvert : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString().Split('|')[0];
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class NotConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }

        #endregion
    }

    public class BooleanToVisibility : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((Visibility)value == Visibility.Visible) ? true : false;
        }

        #endregion
    }

    public class MultiplyConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int multiBy;
            int.TryParse((string)parameter, out multiBy);
            decimal val = (decimal)value;
            return (val * multiBy);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int multiBy;
            int.TryParse((string)parameter, out multiBy);
            decimal val = (decimal)value;
            return (val / multiBy);
        }

        #endregion
    }

    public class EnumListConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var enumType = value.GetType();
            var names = new List<object>();
            foreach (var fieldInfo in enumType.GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                names.Add(fieldInfo.GetValue(null));
            }
            return names;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
